#!/usr/bin/env python3

import glob
import os
from collections import defaultdict


gen_dir = 'group'
lvs_dir = 'lvs_file'

gen_vip_info = {}
lvs_vip_info = {}


def is_list_in(a, b):
        ret = [False for i in a if i not in b]
        return True if len(ret) == 0 else False


def get_realserver_info(file_name):
        realserver_list = []
        fd = open(file_name, 'r');
        for line in fd:
                if line.strip().startswith('#'): continue
                if 'real_server' not in line.strip(): continue
                rip, rport = line.split()[1:3]
                realserver_list.append(rip + ':' + rport)
        fd.close()
        return realserver_list


def get_vip_info(group_name):
        ret = {}
        vip_vport_list = []
        for path in glob.glob(os.path.join(group_name, '*-*.cfg')):
                filename = os.path.basename(path)
                vip   = filename.split('-')[0]
                vport = filename.split('-')[-1].strip('.cfg')
                rs = get_realserver_info(os.path.join(group_name, vip + '-' + vport + '.cfg'))
                ret[(vip, vport)] = rs
        return ret


def compare_vip_only():
        gen_vport_valid = {key:1 for key,value in gen_vip_info.items() if len(value) > 0}
        lvs_vport_valid = {key:1 for key,value in lvs_vip_info.items()   if len(value) > 0}
        if lvs_vport_valid == gen_vport_valid:
                print('vport: same')
        elif is_list_in(lvs_vport_valid.keys(), gen_vport_valid.keys()):
                print('vport: add %s' % (len(gen_vport_valid.keys()) - len(lvs_vport_valid.keys())))
        elif is_list_in(gen_vport_valid.keys(), lvs_vport_valid.keys()):
                print('vport: del %s' % (len(lvs_vport_valid.keys()) - len(gen_vport_valid.keys())))
        else:
                compare_del = [i for i in lvs_vport_valid.keys() if i not in gen_vport_valid.keys()]
                compare_add = [i for i in gen_vport_valid.keys() if i not in lvs_vport_valid.keys()]
                print('vport: del %s & add %s' % (len(compare_del), len(compare_add)));


def compare_rs_only(vport):
        gen_list = gen_vip_info[vport]
        lvs_list = lvs_vip_info[vport]
        if len(gen_list) == len(lvs_list) and is_list_in(lvs_list, gen_list):
                print(vport[0], ':', vport[1], 'rs same')
        elif len(gen_list) > len(lvs_list) and is_list_in(lvs_list, gen_list):
                print(vport[0], ':', vport[1], 'add rs %s' % (len(gen_list) - len(lvs_list)))
        elif len(lvs_list) > len(gen_list) and is_list_in(gen_list, lvs_list):
                print(vport[0], ':', vport[1], 'del rs %s' % (len(lvs_list) - len(gen_list)))
        else:
                compare_del = [i for i in lvs_list if i not in gen_list]
                compare_add = [i for i in gen_list if i not in lvs_list]
                print(vport[0], ':', vport[1], 'del %s & add %s' % (len(compare_del), len(compare_add)));                


if __name__ == '__main__':
        gen_vip_info = get_vip_info(gen_dir)
        lvs_vip_info = get_vip_info(lvs_dir)
        compare_vip_only()
        for i in lvs_vip_info.keys():
                compare_rs_only(i)

